﻿using System;
using System.Data;
using System.Linq;
using Javavaj.iHunter;
using Javavaj.iHunter.Core;

namespace Javavaj.iHunter.Database
{

    public partial class UserDbDataSet
    {
        [Flags]
        private enum MessageStatus
        {
            Normal = 0,
            Deleted = 1,
            Spam = 2
        }
        partial class MessagesDataTable
        {
            /// <summary>
            /// 通过消息Id获取所属的附件记录集合
            /// </summary>
            /// <param name="messageId">消息Id</param>
            /// <returns>附件记录集合</returns>
            public UserDbDataSet.AttachmentsRow[] GetAttachmentsRows(int messageId)
            {
                UserDbDataSet.MessagesRow row = FindById(messageId);
                if (row == null)
                    return null;
                else
                    return row.GetAttachmentsRows();
            }

            public string[] GetCategories(int channelHash)
            {
                var result = from message in this
                             where message.ChannelHash == channelHash
                             select message.Category;
                return result.Distinct().ToArray();
            }
            public int NewMessage()
            {
                UserDbDataSet.MessagesRow row = this.NewMessagesRow();
                this.AddMessagesRow(row);
                row.AcceptChanges();
                return row.Id;
            }
            public bool DeleteMessage(int messageId)
            {
                UserDbDataSet.MessagesRow row = this.FindById(messageId);
                if (row == null)
                    return false;

                row.Delete();
                AcceptChanges();
                return true;
            }
            public int FindByUid(int channelHash, int uid)
            {
                var messages = (from msg in this
                                where channelHash == msg.ChannelHash && uid == msg.Uid
                              select msg).ToArray();
                if (messages.Length == 0)
                    return 0;
                else
                    return messages[0].Id;
            }
            public bool UpdateMessage(IMessage message)
            {
                UserDbDataSet.MessagesRow row = FindById(message.Id);
                if (row == null)
                    return false;

                try
                {
                    row.Author = message.Author;
                    row.Category = message.Category;
                    row.ChannelHash = message.ChannelHash;
                    row.ContentType = message.ContentType;
                    row.Description = message.Description;
                    row.DueTime = message.DueTime;
                    row.Importance = message.Importance;
                    row.LastBuildTime = message.LastBuildTime;
                    row.Link = message.Link;
                    row.PubTime = message.PubTime;
                    row.Read = message.Read;
                    row.Repliable = message.Repliable;
                    row.Title = message.Title;
                    row.Uid = message.Uid;
                    if (message.ContentIsBinary)
                    {
                        row.ContentIsBinary = true;
                        row.TextContent = null;
                        row.BinaryContent = (byte[])message.Content;
                    }
                    else
                    {
                        row.ContentIsBinary = false;
                        row.TextContent = (string)message.Content;
                        row.BinaryContent = null;
                    }

                    MessageStatus status = 0;
                    if (message.Spam)
                        status |= MessageStatus.Spam;
                    if (message.Deleted)
                        status |= MessageStatus.Deleted;

                    row.Status = (int)status;

                    AcceptChanges();

                }
                catch
                {
                    //撤销更改
                    this.RejectChanges();
                    return false;
                }

                return true;
            }
        }
        partial class AttachmentsRow
        {
            public IFileObject CreateAttachment()
            {
                return new Attachment(Id, Filename, Type, Size, Uri, LocalPath, Description);
            }
        }
        partial class MessagesRow
        {
            public IMessage CreateMessageHeader()
            {
                return new MessageHeader()
                {
                    Author = this.Author,
                    Category = this.Category,
                    ChannelHash = this.ChannelHash,
                    Description = this.Description,
                    DueTime = this.DueTime,
                    Id = this.Id,
                    Importance = this.Importance,
                    LastBuildTime = this.LastBuildTime,
                    Link = this.Link,
                    PubTime = this.PubTime,
                    Read = this.Read,
                    Deleted = ((this.Status & (int)MessageStatus.Deleted) != 0),
                    Spam = ((this.Status & (int)MessageStatus.Spam) != 0),
                    Repliable = this.Repliable,
                    Title = this.Title,
                    Uid = this.Uid
                };
            }
            
            public IMessage CreateMessage()
            {
                var attachmentResult = from attachmentRow in this.GetAttachmentsRows()
                                       select new Attachment(
                                           attachmentRow.Id,
                                           attachmentRow.Filename,
                                           attachmentRow.Type,
                                           attachmentRow.Size,
                                           attachmentRow.Uri,
                                           attachmentRow.LocalPath,
                                           attachmentRow.Description
                                           );
                IFileObject[] attachments = attachmentResult.ToArray();

                return new Message(null)
                {
                    Attachments = attachments,
                    Author = this.Author,
                    Category = this.Category,
                    ChannelHash = this.ChannelHash,
                    Content = ((this.ContentIsBinary) ? ((object)this.BinaryContent) : ((object)this.TextContent)),
                    ContentType = this.ContentType,
                    Description = this.Description,
                    DueTime = this.DueTime,
                    Id = this.Id,
                    Importance = this.Importance,
                    LastBuildTime = this.LastBuildTime,
                    Link = this.Link,
                    PubTime = this.PubTime,
                    Read = this.Read,
                    Deleted = ((this.Status & (int)MessageStatus.Deleted) != 0),
                    Spam = ((this.Status & (int)MessageStatus.Spam) != 0),
                    Repliable = this.Repliable,
                    Title = this.Title,
                    Uid = this.Uid
                };
            }

        }

    }
}
